local xieyu = fk.CreateSkill{
  name = "wk_heg__xieyu",
}

local H = require "packages/ol_hegemony/util"
local HG = require "packages/new_heg_diy_my/util"

Fk:loadTranslationTable{
  ["wk_heg__xieyu"] = "协御",
  [":wk_heg__xieyu"] = "①与你势力相同的角色失去体力后，若其<a href= ':Heg__policy'>“战策”</a>数小于其体力上限，你可令其获得一张<a href= ':Heg__policy'>“战策”</a>；②结束阶段，你可选择一名其他角色，直至你下回合开始，当你/其受到【杀】造成的伤害时，其/你可失去1点体力防止之。",
  ["#wk_heg__xieyu_choose"] = "协御：你可选择一名其他角色，直至你下回合开始，当你/其受到【杀】造成的伤害时，其/你可失去1点体力防止之",
  ["@@wk_heg__xieyu_from"] = "协御",
  ["@@wk_heg__xieyu_to"] = "协御",
  ["#wk_heg__xieyu-get"] = "协御：你可令 %dest 获得一张战策",
  ["#wk_heg__xieyu-loseHp"] = "协御：%dest 受到伤害，你可以失去1点体力防止之",

  ["$wk_heg__xieyu1"] = "敌众我寡，怎可少谋？",
  ["$wk_heg__xieyu2"] = "临城据守，当出奇计。",
}

xieyu:addEffect(fk.HpLost, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(xieyu.name) and H.compareKingdomWith(player, target) and HG.GetPolicyNum(target) < target.maxHp
  end,
  on_cost = function (self, event, target, player, data)
  if player.room:askToSkillInvoke(player, {
    skill_name = xieyu.name,
    prompt = "#wk_heg__xieyu-get::"..target.id,
  }) then
    event:setCostData(self, {tos = {target}})
    return true
  end
end,
  on_use = function (self, event, target, player, data)
    HG.GetPolicy(player.room, target, 1, self.name)
  end,
})

xieyu:addEffect(fk.EventPhaseStart, {
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(xieyu.name) and player.phase == Player.Finish
  end,
  on_cost = function (self, event, target, player, data)
    local tos = player.room:askToChoosePlayers(player, {
      targets = player.room:getOtherPlayers(player),
      min_num = 1,
      max_num = 1,
      skill_name = xieyu.name,
      prompt = "#wk_heg__xieyu_choose",
    })
    if #tos > 0 then
      event:setCostData(self, {tos = tos})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    room:addTableMarkIfNeed(player, "@@wk_heg__xieyu_to", to.id) --对别人协御
    room:addTableMarkIfNeed(to, "@@wk_heg__xieyu_from", player.id) -- 被协御
  end,
})

xieyu:addEffect(fk.TurnStart, {
  can_refresh = function (self, event, target, player, data)
    return player == target and #player:getTableMark("@@wk_heg__xieyu_to") > 0
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    for _, toid in ipairs(player:getTableMark("@@wk_heg__xieyu_to")) do
      room:removeTableMark(player, "@@wk_heg__xieyu_to", toid)
      room:removeTableMark(room:getPlayerById(toid), "@@wk_heg__xieyu_from", player.id)
    end
  end,
})

xieyu:addEffect(fk.Death, {
  can_refresh = function (self, event, target, player, data)
    return player == target and (#player:getTableMark("@@wk_heg__xieyu_to") > 0 or #player:getTableMark("@@wk_heg__xieyu_from") > 0)
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    for _, toid in ipairs(player:getTableMark("@@wk_heg__xieyu_to")) do
      room:removeTableMark(player, "@@wk_heg__xieyu_to", toid)
      room:removeTableMark(room:getPlayerById(toid), "@@wk_heg__xieyu_from", player.id)
    end
    for _, fromid in ipairs(player:getTableMark("@@wk_heg__xieyu_from")) do
      room:removeTableMark(player, "@@wk_heg__xieyu_from", fromid)
      room:removeTableMark(room:getPlayerById(fromid), "@@wk_heg__xieyu_to", player.id)
    end
  end,
})

xieyu:addEffect(fk.DetermineDamageInflicted, {
  is_delay_effect = true,
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    return data.card and data.card.trueName == "slash" and (table.contains(target:getTableMark("@@wk_heg__xieyu_from"), player.id) or
      table.contains(target:getTableMark("@@wk_heg__xieyu_to"), player.id))
  end,
  on_cost = function (self, event, target, player, data)
    if player.room:askToSkillInvoke(player, {
      skill_name = xieyu.name,
      prompt = "#wk_heg__xieyu-loseHp::"..target.id,
    }) then
      event:setCostData(self, {tos = {target}})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:loseHp(player, 1, xieyu.name)
    data:preventDamage()
  end,
})
return xieyu